package org.redr.widgetGUI.core;

import org.redr.widgetGUI.settings.WidgetSettings;
import org.redr.widgetGUI.settings.WidgetState;

import com.trolltech.qt.gui.QLayout;
import com.trolltech.qt.gui.QPushButton;

public class Button extends WidgetState{
	
	QPushButton button;
	
	public QPushButton widget(){
		return button;
	}
	
	public Button(LayoutState p, WidgetSettings ws){
		super(p, ws);
		button = new QPushButton(_getSetting("label", "!!FIX ME label SHOULD BE SET!!"));
		button.setCheckable(_getCheckable());
		button.setChecked(_getChecked());
		p.addWidget(button);
	}
	
	public Button(QLayout layout, WidgetSettings ws) {
		super(layout, ws);
		button = new QPushButton(_getSetting("label", "!!FIX ME label SHOULD BE SET!!"));
		button.setCheckable(_getCheckable());
		button.setChecked(_getChecked());
		layout.addWidget(button);
	}

	private boolean _getCheckable(){
		if (_getSetting("checkable", "false").equals("true")) return true;
		else return false;
	}
	
	private boolean _getChecked(){
		if (_getSetting("checked", "false").equals("true")) return true;
		else return false;
	}
	
	public boolean checked(){
		_setState();
		return widget().isChecked();
	}
	
	public void setChecked(boolean c){
		if (widget().isCheckable()){
			widget().setChecked(c);
			_setState();
		} else return;
	}
	
	private void _setState(){
		if (widget().isCheckable()) _setSetting("checkable", "true");
		else _setSetting("checkable", "false");
		if (widget().isChecked())	_setSetting("checked", "true");
		else _setSetting("checkable", "false");
	}

}
